<?php

/**
 * AmplifyCMS
 *
 * This file is part of AmplifyCMS.
 *
 * AmplifyCMS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * AmplifyCMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

require_once('users_amp_controller.php');

class RolesController extends UsersAmpController
{

  /**
   *
   */
  protected function action_index()
  {
    parent::action_index();
    
    $this->response->set('roles', $this->db->from('Role')->orderBy($this->response->get('indexOrderBy'))->offset($this->response->get('indexOffset'))->limit($this->response->get('indexLimit'))->find());
    
    $caps = $this->db->from('Cap')->find();
    $this->response->set('caps', $caps);
    
    $policy = $this->response->get('policy');
    
    $data = array();
    foreach ($caps as $cap) {
      $data[$cap->id] = __($cap->name) . ' (' . $policy[$cap->policy] . ')';
    }
    $this->response->set('caps', $data);
  }

  /**
   *
   */
  protected function action_view($id = null)
  {
    try {
      $this->validateId($id);
      $this->response->set('role', $this->db->load('Role', $id));
    } 

    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
      return $this->response->redirect(Request::makeUrl('/users/roles'));
    } 

    catch (NotFoundException $e) {
      $this->response->warning(__('Role not found'));
      return $this->response->redirect('/users/roles');
    }
  }

  /**
   *
   */
  protected function action_create()
  {
    return $this->response->redirectIf($this->form() !== false, '/users/roles');
  }

  /**
   *
   */
  protected function action_edit($id = null)
  {
    try {
      $this->validateId($id);
      return $this->response->redirectIf($this->form($id) !== false, '/users/roles');
    } 

    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
      return $this->response->redirect('/users/roles');
    } 

    catch (NotFoundException $e) {
      $this->response->warning(__('Role not found'));
      return $this->response->redirect('/users/roles');
    }
  }

  /**
   *
   */
  protected function action_delete($id = null)
  {
    try {
      $this->validateId($id);
      $role = $this->db->load('Role', $id);
      $this->db->delete($role);
      $this->response->notice(__('Role deleted'));
    } 

    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
    } 

    catch (NotFoundException $e) {
      $this->response->warning(__('Role not found'));
    }
    
    return $this->response->redirect('/users/roles');
  }

  /**
   *
   */
  protected function action_toggle_cap($id = null, $cap_id = null)
  {
    try {
      $this->validateId($id);
      $this->validateId($cap_id);
      
      $role = $this->db->load('Role', $id);
      $caps = $role->getCaps();
      
      $c = $caps->count();
      $i = 0;
      while ($i < $c && $caps[$i]->id != $cap_id)
        $i ++;
      
      $_caps = $caps->toArray();
      
      if ($i < $c) {
        unset($_caps[$i]);
        
        $caps->removeAll();
        $caps->exchangeArray($_caps);
        $this->db->save($role);
        
        $this->response->notice(__('Capability removed'));
      }
      else {
        $_caps[] = $this->db->load('Cap', $cap_id);
        
        $caps->removeAll();
        $caps->exchangeArray($_caps);
        $this->db->save($role);
        
        $this->response->notice(__('Capability added'));
      }
    } 

    catch (ParameterException $e) {
      $this->response->warning($e->getMessage());
    } 

    catch (NotFoundException $e) {
      $this->response->warning(__('Role or capability not found'));
    }

    $this->setTemplate(false);
  }

  /**
   *
   */
  private function form($id = null)
  {
    if (empty($id)) {
      $role = new Role();
      $this->response->set('formTitle', __('Create Role'));
    }
    else {
      $role = $this->db->load('Role', $id);
      $this->response->set('formTitle', __('Edit Role'));
    }
    
    $this->response->set('role', $role);
    
    $levelData = array();
    for ($i = 2; $i < 99; $i ++)
      $levelData[$i] = $i;
    $this->response->set('levelData', $levelData);
    
    if ($this->form) {
      try {
        $role->copyAll($this->form);
        $role->validateAll();
        $this->db->save($role);
        $this->response->notice(__('Data saved'));
        
        return $role;
      } 

      catch (EmptyException $e) {
        $this->response->notice(__('Nothing to save'));
      } 

      catch (ValidationException $e) {
        $this->response->warning($role->getErrors());
      }
    }
    
    $this->setTemplate('roles_form');
    
    return false;
  }
}

?>